#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include<vector>
#include<string>

using namespace std;
int find() {
    int a = 0;
    cin >> a;
    string flag1;
    string flag2;
    cin >> flag1;
    cin >> flag2;
    if (flag1.c_str() == nullptr && flag2.c_str() == nullptr)
    {
        return -1;
    }

    vector<string> ret;
    ret.reserve(a);
    int k = 0;
    int j = 0;
    int ret1 = 10000000;
    for (int i = 0; i < ret.size(); i++) {
        cin >> ret[i];
        if (flag1 == ret[i]) {
            k = i;
        }
        if (flag2 == ret[i]) {
            j = i;
        }
        int tmp = abs(k - j);
        if (tmp != 0 && tmp < ret1)
        {
            ret1 = tmp;
        }

    }

    if (!ret1 && ret1 != 10000000)
    {
        return ret1;
    }
    else {
        return -1;
    }

}

int main() {

    cout << find();
    return 0;


}